//
//  PostDetailCell.m
//  sensen_ios
//
//  Created by pro on 16/3/2.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "PostDetailCell.h"

#import "PostDetailImgsView.h" // 图片
#import "ZKTextImgReplaceHandle.h"
@interface PostDetailCell ()
{
    UILabel *_titleLabel; // 帖子标题
    UIImageView *_timeImgView; // 时间图片
    UILabel *_timeLabel; // 发表时间 //论坛_进入论坛_时间
    UILabel *_contentLabel; // 帖子内容
    PostDetailImgsView *_postImgsView; // 图片
    
//    UIButton *_favourButton; // 点赞图片
    UILabel *_favourLab;//点赞数
}


@end

@implementation PostDetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self _initViews];
    }
    return self;
}

#pragma mark -- 创建视图 --
- (void)_initViews {
   
    CGFloat x,y,width,height;
    
    // 头像
    x = 10;
    y = 5;
    width = 30;
    height = width;
    
    // 标题
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = B_FONT(18);
    _titleLabel.text = @"发帖标题";
    _titleLabel.textColor = SS_NAVI_BLUE_COLOR;
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    _titleLabel.numberOfLines = 0;
    [self.contentView addSubview:_titleLabel];
    
    // 内容
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.font = FONT(16);
    _contentLabel.numberOfLines = 0;
    _contentLabel.text = @"发帖发帖内容发帖内容发帖内容发帖内容发帖内容发帖内容发帖内容";
    _contentLabel.textColor = [UIColor darkGrayColor];
    _contentLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_contentLabel];
    
    // 发表时间
    _timeImgView = [[UIImageView alloc] init];
    _timeImgView.image = [UIImage imageNamed:@"论坛_进入论坛_时间"];
    [self.contentView addSubview:_timeImgView];
    _timeLabel = [[UILabel alloc] init];
    _timeLabel.font = FONT(13);
    _timeLabel.text = @"创建于5天前";
    _timeLabel.textColor = [UIColor darkGrayColor];
    _timeLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_timeLabel];
    
    // 图片视图
    _postImgsView = [[PostDetailImgsView alloc] init];
    _postImgsView.tag = 3000;
    _postImgsView.imgAry = @[];
    [self.contentView addSubview:_postImgsView];
    
    _favourButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_favourButton setImage:[UIImage imageNamed:@"论坛_进入论坛_赞2"] forState:UIControlStateNormal];
   
    [_favourButton addTarget:self action:@selector(_doFavour:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_favourButton];
    _favourLab = [[UILabel alloc] init];
    _favourLab.font = FONT(18);
    _favourLab.text = @"666";
    _favourLab.textAlignment = NSTextAlignmentLeft;
    _favourLab.textColor = [UIColor  darkGrayColor];
    [self.contentView addSubview:_favourLab];
    
}


#pragma mark - 重写set
- (void)setLayout:(PostCellFrameLayout *)layout {
    
    _layout = layout;
    
    [self _setAttributeAndLayout];
}

// 设置属性和布局
- (void)_setAttributeAndLayout {
    
    PostModel *model = _layout.model;
    // 设置标题
    _titleLabel.frame = _layout.titleRect;
    _titleLabel.text = model.postTitle;
    // 设置时间
    _timeImgView.frame = _layout.timeImgRect;
    _timeLabel.frame = _layout.timeRect;
    
    NSNumber *dateNub = [NSNumber numberWithFloat:[model.dateline floatValue]];
    NSString *dateStr = [NSDate getDateStringFromeNumber:dateNub];
    _timeLabel.text = dateStr;
    
    // 设置内容视图
    _contentLabel.frame = _layout.contentRect;
    NSMutableAttributedString *attr = [ZKTextImgReplaceHandle handleText:model.postContent withRegex:nil];
    if (attr) {
        _contentLabel.attributedText = attr;
    } else {
        _contentLabel.text = model.postContent;
    }
    
    // 图片
    _postImgsView.imgHeightAry = _layout.imgHeightAry;
    _postImgsView.imgAry = model.imgAry;
    _postImgsView.frame = _layout.imgRect;
    
    _favourButton.frame = _layout.doFavourRect;
    _favourLab.frame = _layout.doFavLabRect;
    _favourLab.text = model.likes;
//    li = [_layout.model.likes intValue];

}


//点赞
- (void)_doFavour:(UIButton *)button{
//    UserModel *user = [CommonCache getUserInfo];
//    if (user == nil) {
//        
//    }else{
//       [_favourButton setImage:[UIImage imageNamed:@"论坛_进入论坛_赞3"] forState:UIControlStateNormal];
////        li = li + 1;
////        NSString * str = [NSString stringWithFormat:@"%ld",(long)li];
////        _favourLab.text = str;
//        
//    }
    [self.delegate favourButtonDid:button];
}

@end
